package com.google.android.exoplayer2.upstream;

import android.content.Context;
import android.content.res.AssetFileDescriptor;
import android.content.res.Resources;
import android.net.Uri;
import android.text.TextUtils;
import androidx.annotation.Nullable;
import java.io.EOFException;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import o.alf;

public final class RawResourceDataSource extends alf {
    private boolean ʽ;
    @Nullable
    private Uri ˊ;
    private final Resources ˋ;
    private long ˎ;
    @Nullable
    private AssetFileDescriptor ˏ;
    @Nullable
    private InputStream ॱ;

    public static class RawResourceDataSourceException extends IOException {
        public RawResourceDataSourceException(String str) {
            super(str);
        }

        public RawResourceDataSourceException(IOException iOException) {
            super(iOException);
        }
    }

    public RawResourceDataSource(Context context) {
        super(false);
        this.ˋ = context.getResources();
    }

    public long ˋ(DataSpec dataSpec) throws RawResourceDataSourceException {
        long j = -1;
        try {
            this.ˊ = dataSpec.ˎ;
            if (TextUtils.equals("rawresource", this.ˊ.getScheme())) {
                int parseInt = Integer.parseInt(this.ˊ.getLastPathSegment());
                ˊ(dataSpec);
                this.ˏ = this.ˋ.openRawResourceFd(parseInt);
                this.ॱ = new FileInputStream(this.ˏ.getFileDescriptor());
                this.ॱ.skip(this.ˏ.getStartOffset());
                if (this.ॱ.skip(dataSpec.ॱॱ) < dataSpec.ॱॱ) {
                    throw new EOFException();
                }
                if (dataSpec.ᐝ != -1) {
                    this.ˎ = dataSpec.ᐝ;
                } else {
                    long length = this.ˏ.getLength();
                    if (length != -1) {
                        j = length - dataSpec.ॱॱ;
                    }
                    this.ˎ = j;
                }
                this.ʽ = true;
                ˎ(dataSpec);
                return this.ˎ;
            }
            throw new RawResourceDataSourceException("URI must use scheme rawresource");
        } catch (NumberFormatException e) {
            throw new RawResourceDataSourceException("Resource identifier must be an integer.");
        } catch (IOException e2) {
            throw new RawResourceDataSourceException(e2);
        }
    }

    public int ˋ(byte[] bArr, int i, int i2) throws RawResourceDataSourceException {
        if (i2 == 0) {
            return 0;
        }
        if (this.ˎ == 0) {
            return -1;
        }
        try {
            if (this.ˎ != -1) {
                i2 = (int) Math.min(this.ˎ, (long) i2);
            }
            int read = this.ॱ.read(bArr, i, i2);
            if (read != -1) {
                if (this.ˎ != -1) {
                    this.ˎ -= (long) read;
                }
                ˊ(read);
                return read;
            } else if (this.ˎ == -1) {
                return -1;
            } else {
                throw new RawResourceDataSourceException(new EOFException());
            }
        } catch (IOException e) {
            throw new RawResourceDataSourceException(e);
        }
    }

    @Nullable
    public Uri ˊ() {
        return this.ˊ;
    }

    public void ॱ() throws RawResourceDataSourceException {
        this.ˊ = null;
        try {
            if (this.ॱ != null) {
                this.ॱ.close();
            }
            this.ॱ = null;
            try {
                if (this.ˏ != null) {
                    this.ˏ.close();
                }
                this.ˏ = null;
                if (this.ʽ) {
                    this.ʽ = false;
                    ˏ();
                }
            } catch (IOException e) {
                throw new RawResourceDataSourceException(e);
            } catch (Throwable th) {
                this.ˏ = null;
                if (this.ʽ) {
                    this.ʽ = false;
                    ˏ();
                }
            }
        } catch (IOException e2) {
            throw new RawResourceDataSourceException(e2);
        } catch (Throwable th2) {
            this.ॱ = null;
            try {
                if (this.ˏ != null) {
                    this.ˏ.close();
                }
                this.ˏ = null;
                if (this.ʽ) {
                    this.ʽ = false;
                    ˏ();
                }
            } catch (IOException e22) {
                throw new RawResourceDataSourceException(e22);
            } catch (Throwable th3) {
                this.ˏ = null;
                if (this.ʽ) {
                    this.ʽ = false;
                    ˏ();
                }
            }
        }
    }
}
